/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc;

import com.cxhd.dc.actor.ActorManager;
import com.cxhd.dc.task.ScanFileToZipTask;
import com.cxhd.dc.task.StartTask;
import com.cxhd.dc.task.UploadFileTask;
import com.cxhd.dc.tool.CxhdTran;
import com.cxhd.dc.tool.FileUtil;
import com.cxhd.dc.tool.HttpClientUt;
import com.cxhd.dc.tool.SqlFile;
import com.cxhd.dc.tool.StreamGobbler;
import com.cxhd.dc.tool.Tools;
import com.cxhd.dc.websocket.ConnectServer;
import com.google.common.io.Files;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import it.sauronsoftware.ftp4j.FTPClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 287257162 Pacs.1234
 *
 * @author Administrator
 */
public class Test {

    public static void main(String[] args) throws Exception {
        String str = "CX0712131CN2930_1.2.156.110001.1.4899.925957.20161125_20161125_102234_4326.rpt";
        System.out.println(str.split("_")[1]);
//     ActorManager  actorManager = ActorManager.getIntance();
//        actorManager.initActor();
//        System.out.println("启动Actor");
      //  cmd();
        // sendMessage("test-------", "base.tronsystem.com:19027", "147","影像中心");
        // sendMessage("test-------", "127.0.0.1:19027", "8","影像中心");
        // System.out.println(Files.getNameWithoutExtension("1233333.jar"));
        //System.out.println(HttpClientUt.download("D:\\123\\1233333.jar", "http://183.232.150.37:22080/CXFS/download?fileId=group1/M02/00/00/wKhY8VYpp5WABhL0AFaP7Aa-c7M077.jar"));
        // CxhdTran.getInstance().upload("test", false, new File("D:\\123\\123.exe"));
        // System.out.println(ScanFileToZipTask.getStudyUDIDForXml("K:\\STEVE\\tempUnZipFolder\\9C7992DF.xml"));
        // initJfinalPlugins();
//        StartTask.getIntance().start();
        // Timer uf = new Timer();
        //uf.schedule(new UploadFileTask(), 1000, 1000 );

        // ConnectServer con = new ConnectServer();
        // con.reConnect();
    }

    private static void initJfinalPlugins() {

        // 配置C3p0数据库连接池插件
        DruidPlugin druidPlugin_global_med = new DruidPlugin("jdbc:mysql://127.0.0.1/dcmris_en?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull", "root", "efotoadmin");

        // 配置ActiveRecord插件
        ActiveRecordPlugin arp_global_med = new ActiveRecordPlugin("dcmris_en", druidPlugin_global_med);
        arp_global_med.setDialect(new MysqlDialect());

        druidPlugin_global_med.start();
        arp_global_med.start();

        if (Db.use("dcmris_en").query("SELECT table_name FROM information_schema.TABLES WHERE table_name ='dataconfig'").isEmpty()) {

            Db.use("dcmris_en").update(SqlFile.dataConfig);
        }
        DataConfig.getInstance().getConfigForDatabase().checkFolder();

    }

    public static void cmd() {
        try {
            Runtime r = Runtime.getRuntime();
            Process p = r.exec("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device cxhd Atlas1@127.0.0.1:11118 c:/dcmsyst/cache/1445848195119.dcm ");
            //logger.info("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + this.pacsInfo[2] + "@" + this.pacsInfo[0] + ":" + this.pacsInfo[1] + " " + newFloder.getAbsolutePath() + " -set 00080005= \"GB18030\" -set 00080080=\"" + ApProperties.getSNtoHospitalName(device) + "\"");
            //System.out.println("cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + this.pacsInfo[2] + "@" + this.pacsInfo[0] + ":" + this.pacsInfo[1] + " " + s.getAbsolutePath());
            //writeText("c:/dcmsyst/send.txt", "cmd.exe /c C:\\dcmsyst\\tool-2.0.26\\bin\\dcmsnd -device " + device + " " + this.pacsInfo[2] + "@" + this.pacsInfo[0] + ":" + this.pacsInfo[1] + " " + newFloder.getAbsolutePath());
            StreamGobbler errorGobbler = new StreamGobbler(p.getErrorStream(), "ERROR",null,null);
            errorGobbler.start();
            StreamGobbler outGobbler = new StreamGobbler(p.getInputStream(), "STDOUT",null,null);
            outGobbler.start();
            p.waitFor();
            p.destroy();
        } catch (Exception ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void sendMessage(String message, String url, String groupId, String from) {

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("groupId", groupId));
        params.add(new BasicNameValuePair("contact", message));
        params.add(new BasicNameValuePair("from", from));
        httpPost("http://" + url + "/app/message/sendGroupMessage.data", params);

    }

    public static String httpPost(String url, List<NameValuePair> list) {
        String result = null;
        try {
            // (1) Post请求
            HttpPost post = new HttpPost(url);

//            List<NameValuePair> params = new ArrayList<NameValuePair>();  
//params.add(new BasicNameValuePair("name", "ahopedog"));  
//params.add(new BasicNameValuePair("work", "程序员"));  
//            
            post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
            CloseableHttpClient http = null;
            CloseableHttpResponse response = null;
            try {
                // (3) 发送请求
                http = HttpClients.createDefault();
                response = http.execute(post);

                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    result = EntityUtils.toString(entity, HTTP.UTF_8);

                    System.out.println("-----------------postresult:" + result);
                    return result;
                }
            } finally {
                response.close();
                http.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
}
